package gui;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import net.proteanit.sql.DbUtils;
import codes.DBBean;


public class WebPage extends javax.swing.JFrame {
	 String queryDelete;
	 String queryUpdate;
	 public static String urlUpdate;
	 public static String keywordsUpdate;
	 public static String titleUpdate;
	 public static String descriptionUpdate;
  
   public WebPage() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
       initComponents();
   }

                           
   private void initComponents() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {

       jScrollPane1 = new javax.swing.JScrollPane();
       jTable1 = new javax.swing.JTable();
       jLabel1 = new javax.swing.JLabel();
       insertButton = new javax.swing.JButton();
       deleteButton = new javax.swing.JButton();
       updateButton = new javax.swing.JButton();
       keywordButton = new javax.swing.JButton();
       returnButton = new javax.swing.JButton();
       exitButton = new javax.swing.JButton();

       setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

       jTable1.setModel(new javax.swing.table.DefaultTableModel(
           new Object [][] {
               {null, null, null, null},
               {null, null, null, null},
               {null, null, null, null},
               {null, null, null, null}
           },
           new String [] {
               "URL", "Title", "Description", "Keywords"
           }
       ));
       jScrollPane1.setViewportView(jTable1);

       jLabel1.setText("Web Page");

       insertButton.setText("Insert");
       insertButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               insertButtonActionPerformed(evt);
           }
       });

       deleteButton.setText("Delete");
       deleteButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               try {
				deleteButtonActionPerformed(evt);
			} catch (InstantiationException | IllegalAccessException
					| ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
           }
       });

       updateButton.setText("Update");
       updateButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               updateButtonActionPerformed(evt);
           }
       });

       keywordButton.setText("List Keywords");
       keywordButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               keywordButtonActionPerformed(evt);
           }
       });

       returnButton.setText("Return");
       returnButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               returnButtonActionPerformed(evt);
           }
       });

       exitButton.setText("Exit");
       exitButton.addActionListener(new java.awt.event.ActionListener() {
           public void actionPerformed(java.awt.event.ActionEvent evt) {
               exitButtonActionPerformed(evt);
           }
       });

       javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
       getContentPane().setLayout(layout);
       layout.setHorizontalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                   .addGroup(layout.createSequentialGroup()
                       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                           .addGroup(layout.createSequentialGroup()
                               .addContainerGap()
                               .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 435, javax.swing.GroupLayout.PREFERRED_SIZE))
                           .addGroup(layout.createSequentialGroup()
                               .addGap(186, 186, 186)
                               .addComponent(jLabel1)))
                       .addGap(0, 1, Short.MAX_VALUE))
                   .addGroup(layout.createSequentialGroup()
                       .addGap(25, 25, 25)
                       .addComponent(insertButton)
                       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                       .addComponent(deleteButton)
                       .addGap(42, 42, 42)
                       .addComponent(updateButton)
                       .addGap(27, 27, 27)
                       .addComponent(keywordButton)))
               .addContainerGap())
           .addGroup(layout.createSequentialGroup()
               .addGap(80, 80, 80)
               .addComponent(returnButton)
               .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(exitButton)
               .addGap(106, 106, 106))
       );
       layout.setVerticalGroup(
           layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
           .addGroup(layout.createSequentialGroup()
               .addContainerGap()
               .addComponent(jLabel1)
               .addGap(34, 34, 34)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addGap(45, 45, 45)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(keywordButton)
                   .addComponent(updateButton)
                   .addComponent(deleteButton)
                   .addComponent(insertButton))
               .addGap(27, 27, 27)
               .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                   .addComponent(returnButton)
                   .addComponent(exitButton))
               .addContainerGap(29, Short.MAX_VALUE))
       );

       pack();
       DBBean db = new DBBean();
		Connection con = db.openConnection();

		String query = "SELECT * FROM WebPage";

		PreparedStatement statement = con.prepareStatement(query);

		ResultSet result = statement.executeQuery();

		jTable1.setModel(DbUtils.resultSetToTableModel(result));
		
		jTable1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int row = jTable1.getSelectedRow();
				System.out.println(row);
				DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
				titleUpdate =  model.getValueAt(row, 1).toString();
				urlUpdate = model.getValueAt(row, 0).toString();
				descriptionUpdate = model.getValueAt(row, 2).toString();
				keywordsUpdate = model.getValueAt(row, 3).toString();
				
				queryDelete = "DELETE from WebPage WHERE title = '" + titleUpdate + "'";
				//colocar botao return and notifiacao de sucesso
				
				
			}
		});
   }// </editor-fold>                        

   private void returnButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
      new Menu().setVisible(true);
	    	WebPage.this.dispose();        
   }                                        

   private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
	   System.exit(0);
       
   }                                        

   private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
	   new InsertWebPage().setVisible(true);
       WebPage.this.dispose();
   }                                        

   private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {                                         
	   	DBBean db = new DBBean();
		Connection con = db.openConnection();

		String query = queryDelete;

		PreparedStatement statement = con.prepareStatement(query);
		boolean result = statement.execute();
		JOptionPane.showMessageDialog(null, "Row deleted! ");
		new WebPage().setVisible(true);

   }                                        

   private void updateButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
	   new UpdateWebPage().setVisible(true);
       WebPage.this.dispose();
   }                                        

   private void keywordButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
       // TODO add your handling code here:
   }                                        

  
   public static void main(String args[]) {
      
       try {
           for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
               if ("Nimbus".equals(info.getName())) {
                   javax.swing.UIManager.setLookAndFeel(info.getClassName());
                   break;
               }
           }
       } catch (ClassNotFoundException ex) {
           java.util.logging.Logger.getLogger(WebPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
       } catch (InstantiationException ex) {
           java.util.logging.Logger.getLogger(WebPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
       } catch (IllegalAccessException ex) {
           java.util.logging.Logger.getLogger(WebPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
       } catch (javax.swing.UnsupportedLookAndFeelException ex) {
           java.util.logging.Logger.getLogger(WebPage.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
       }
       //</editor-fold>

       /* Create and display the form */
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               try {
				new WebPage().setVisible(true);
			} catch (InstantiationException | IllegalAccessException
					| ClassNotFoundException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
           }
       });
   }

   // Variables declaration - do not modify                     
   private javax.swing.JButton insertButton;
   private javax.swing.JButton deleteButton;
   private javax.swing.JButton updateButton;
   private javax.swing.JButton keywordButton;
   private javax.swing.JButton returnButton;
   private javax.swing.JButton exitButton;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable jTable1;
   // End of variables declaration                   
}

